package org.jeecg.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.config.wlhy.QuickVisionClient;
import org.jeecg.modules.config.entity.WlhyBank;
import org.jeecg.modules.config.service.IWlhyBankService;
import org.jeecg.modules.user.entity.WlhyUserBank;
import org.jeecg.modules.user.mapper.WlhyUserBankMapper;
import org.jeecg.modules.user.service.IWlhyUserBankService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

import static org.jeecg.common.constant.WlhyBizConstant.ROW_STATUS0;
import static org.jeecg.common.constant.WlhyBizConstant.ROW_STATUS1;

/**
 * @Description: 用户银行卡管理
 * @Author: jeecg-boot
 * @Date: 2024-04-11
 * @Version: V1.0
 */
@Service
public class WlhyUserBankServiceImpl extends ServiceImpl<WlhyUserBankMapper, WlhyUserBank> implements IWlhyUserBankService {

    @Resource
    QuickVisionClient quickVisionClient;
@Resource
    IWlhyBankService wlhyBankService;
    @Override
    public void delete(String id) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        update(new LambdaUpdateWrapper<WlhyUserBank>()
                .eq(WlhyUserBank::getId, id)
                .eq(WlhyUserBank::getUserId, loginUser.getId())
                .set(WlhyUserBank::getStatus, ROW_STATUS0));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(WlhyUserBank wlhyUserBank) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        WlhyBank wlhyBank = wlhyBankService.getById(wlhyUserBank.getBankId());
        if (null == wlhyBank) {
            throw new RuntimeException("银行卡类型不存在");
        }
        Boolean check = wlhyBankService.prefixCheck(Arrays.asList(wlhyBank.getPrefix().split(",")),wlhyUserBank.getBankCardNo());
        if (!check) {
            throw new RuntimeException("卡号前缀不存在,请联系客服");
        }
        Boolean authResult;
        try {
            authResult = quickVisionClient.bankNameIdCardMobile(
                    wlhyUserBank.getBankCardNo(),
                    wlhyUserBank.getBankHolder(),
                    wlhyUserBank.getBankIdNo(),
                    wlhyUserBank.getBankPhoneNo()
            );
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        if (!authResult) {
            throw new RuntimeException("银行卡认证失败");
        }

        update(new LambdaUpdateWrapper<WlhyUserBank>().eq(WlhyUserBank::getUserId, loginUser.getId()).set(WlhyUserBank::getIzDefault, 0));





        wlhyUserBank.setBankName(wlhyBank.getBankName());
        wlhyUserBank.setUserId(loginUser.getId());
        wlhyUserBank.setStatus(ROW_STATUS1);
        wlhyUserBank.setIzDefault(1);
        save(wlhyUserBank);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefault(String id) {
        WlhyUserBank userBank = getById(id);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        if (null == userBank || !userBank.getUserId().equals(loginUser.getId())) {
            throw new RuntimeException("银行卡不存在");
        }
        update(new LambdaUpdateWrapper<WlhyUserBank>().eq(WlhyUserBank::getUserId, loginUser.getId()).set(WlhyUserBank::getIzDefault, 0));
        update(new LambdaUpdateWrapper<WlhyUserBank>().eq(WlhyUserBank::getId, id).set(WlhyUserBank::getIzDefault, 1));
    }

    @Override
    public WlhyUserBank myDefaultBank() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return getOne(new LambdaUpdateWrapper<WlhyUserBank>()
                .eq(WlhyUserBank::getUserId,loginUser.getId())
                .eq(WlhyUserBank::getStatus,ROW_STATUS1)
                .eq(WlhyUserBank::getIzDefault, 1));
    }
}
